import { knowledge } from '@/api/knowledgeBase'
const useInit = (initInfo) => {
  // 初始化页面
  const initPage = () => {
    getListData()
  }

  // 获取当前查询条件下的数据
  const getListData = () => {
    resetPage()
    const result: any = {}
    result.page = initInfo.value.search.page
    result.limit = initInfo.value.search.limit
    result.checknumber = initInfo.value.search.checknumber
    result.checktype_id = initInfo.value.search.checktype_id
    result.baseanimal_id = initInfo.value.search.baseanimal_id
    result.enterprise = initInfo.value.search.enterprise
    result.disease_id = initInfo.value.search.disease_id
    result.checkname = initInfo.value.search.checkname
    if (initInfo.value.search.collectDate && initInfo.value.search.collectDate.length > 0) {
      result.shouyang_starttime = initInfo.value.search.collectDate[0]
      result.shouyang_endtime = initInfo.value.search.collectDate[1]
    } else {
      result.shouyang_starttime = ''
      result.shouyang_endtime = ''
    }
    if (initInfo.value.search.gatherDate && initInfo.value.search.gatherDate.length > 0) {
      result.caiyang_starttime = initInfo.value.search.gatherDate[0]
      result.caiyang_endtime = initInfo.value.search.gatherDate[1]
    } else {
      result.caiyang_starttime = ''
      result.caiyang_endtime = ''
    }
    if (initInfo.value.search.checkDate && initInfo.value.search.checkDate.length > 0) {
      result.check_starttime = initInfo.value.search.checkDate[0]
      result.check_endtime = initInfo.value.search.checkDate[1]
    } else {
      result.check_starttime = ''
      result.check_endtime = ''
    }
    if (initInfo.value.search.IssuedDate && initInfo.value.search.IssuedDate.length > 0) {
      result.report_starttime = initInfo.value.search.IssuedDate[0]
      result.report_endtime = initInfo.value.search.IssuedDate[1]
    } else {
      result.report_starttime = ''
      result.report_endtime = ''
    }
    knowledge.list(result, null).then((res: any) => {
      if (res && res.status) {
        initInfo.value.list.total = res.allcount
        initInfo.value.total.checkNumber = res.allcount
        initInfo.value.total.geti_number = res.geti_number
        initInfo.value.total.geti_yang_number = res.geti_yang_number
        initInfo.value.total.geti_yanglv = res.geti_yanglv + '%'
        if (res.data && res.data.length > 0) {
          initInfo.value.list.data = operateData(res.data)
        }
      }
    })
  }

  // 数据操作
  const operateData = (data) => {
    const result = []
    data.forEach((item: any) => {
      if (item.project && item.project.length > 0) {
        item.project.forEach((v: any, idx) => {
          result.push({
            checknumber: item.checknumber,
            address: v.address,
            animalinfo_name: v.animalinfo_name,
            baseanimal_name: v.baseanimal_name,
            caiyang_time: v.caiyang_time,
            check_method: v.check_method,
            check_time: v.check_time,
            checkname: v.checkname,
            checkproject_name: v.checkproject_name,
            enterprise_name: v.enterprise_name,
            entertype_name: v.entertype_name,
            jiance_name: v.jiance_name,
            jingdu: v.jingdu,
            person: v.person,
            phone: v.phone,
            publishreport_time: v.publishreport_time,
            sampletype_name: v.sampletype_name,
            siyang_number: v.siyang_number,
            town: v.town,
            weidu: v.weidu,
            geti_number: v.geti_number,
            geti_yang_number: v.geti_yang_number,
            geti_yanglv: v.geti_yanglv + '%',
            isStart: idx === 0,
            end: item.project.length
          })
        })
      }
    })
    return result
  }

  // 页面数据条数改变重新获取数据
  const handleSizeChange = (val: number) => {
    initInfo.value.search.limit = val
    initInfo.value.search.page = 1
    getListData()
  }

  // 当前页改变重新获取数据
  const handleCurrentChange = (val: number) => {
    initInfo.value.search.page = val
    getListData()
  }

  // 重置页面数据
  const resetPage = () => {
    initInfo.value.list.data = []
  }

  // 重置
  const resetSearch = () => {
    initInfo.value.search.page = 1
    initInfo.value.search.limit = 20
    initInfo.value.search.checknumber = ''
    initInfo.value.search.checktype_id = ''
    initInfo.value.search.baseanimal_id = ''
    initInfo.value.search.enterprise = ''
    initInfo.value.search.disease_id = ''
    initInfo.value.search.checkname = ''
    initInfo.value.search.collectDate = []
    initInfo.value.search.gatherDate = []
    initInfo.value.search.checkDate = []
    initInfo.value.search.IssuedDate = []
    getListData()
  }

  // 合计
  const getSummaries = (param: any) => {
    const result = []
    param.columns.forEach((column, index) => {
      if (index === 0) {
        result.push('合计：' + initInfo.value.total.checkNumber)
      } else {
        if (column.property === 'geti_number' || column.property === 'geti_yang_number' || column.property === 'geti_yanglv') {
          result.push(initInfo.value.total[column.property])
        } else {
          result.push('')
        }
      }
    })
    return result
  }

  // 合并单元格
  const spanCell = ({ row, column, rowIndex, columnIndex }) => {
    if (column.property === 'checknumber') {
      if (row.isStart) {
        return {
          rowspan: row.end,
          colspan: 1
        }
      } else {
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    }
  }

  return {
    initPage,
    getListData,
    handleSizeChange,
    handleCurrentChange,
    resetSearch,
    operateData,
    getSummaries,
    spanCell
  }
}
export default useInit
